<style type="text/css" title="currentStyle">
	@import "../media/css/demo_page.css";
	@import "../media/css/demo_table_jui.css";
	@import "../examples_support/themes/smoothness/jquery-ui-1.8.4.custom.css";
	@import "media/css/TableTools_JUI.css";
</style>
<script type="text/javascript" charset="utf-8" src="../media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="media/js/TableTools.js"></script>
<script type="text/javascript" charset="utf-8">
	$(document).ready( function () {
		$('#example').dataTable( {
			"bJQueryUI": true,
			"sPaginationType": "full_numbers",
			"sDom": '<"H"Tfr>t<"F"ip>',
			"oTableTools": {
				"aButtons": [
					"copy", "csv", "xls", "pdf", "print",
					{
						"sExtends":    "collection",
						"sButtonText": "Save",
						"aButtons":    [ "csv", "xls", "pdf" ]
					}
				]
			}
		} );
	} );
</script>
<?php
//print_r($_POST);
if(isset($_POST[txtDeviceId]) && $_POST[txtDeviceId] !='') 
{
	if($_POST[txtClientModel] == 1)
	{
		$sql5 = "DELETE FROM tb_route_info WHERE tri_deviceId =".$_POST[txtDeviceId]." AND tri_clientId =".$_POST[txtClientId];
		$db->query($sql5);
	}

 	$sql3 = "DELETE FROM tb_assigngeofence WHERE tag_diId =".$_POST[txtDeviceId]." AND tag_clientId =".$_POST[txtClientId];
	$db->query($sql3);
	
	$sql4 = "DELETE FROM tb_device_alert_info WHERE tdai_deviceId =".$_POST[txtDeviceId]." AND tdai_clientId =".$_POST[txtClientId];
	$db->query($sql4);
	
	$sql2 = "DELETE FROM tb_client_subscription WHERE tcs_deviceId =".$_POST[txtDeviceId]." AND tcs_clientId =".$_POST[txtClientId];
	$db->query($sql2);
	
	$sql1 = "DELETE FROM tb_deviceinfo WHERE di_id =".$_POST[txtDeviceId]." AND di_clientId =".$_POST[txtClientId];	
	if($db->query($sql1))
	{
			header("location:?ch=status&au=20&msg=1");
			exit;
	}
	else
	{
		header("location:?ch=status&au=20&msg=0");
		exit;
	}
}
?>
<script language="javascript">
function funEditUser(uid,cid,mid,act)
{
	t = confirm("You will loose important data related to this device after deleted. Still you want to delete this device ?")
	if(t)
	{
		document.frmSubmit.txtDeviceId.value = uid;
		document.frmSubmit.txtClientId.value = cid;
		document.frmSubmit.txtClientModel.value = mid;
		document.frmSubmit.action = act;
		document.frmSubmit.submit();
	}
}
</script>
<table cellpadding="0" cellspacing="0" border="0" class="gridform_final" id="example">
   <thead>
    <tr>
        <th>Device ID   </th>
        <th>Client Name</th>
        <th>IMEI</th> 
        <th>Device Name</th>
        <th>Mobile No</th>
        <th>Device Model</th>
        <th>C. Date</th> 
        <th>Sub. Exp. Date</th>
		<th>Delete</th>
   </tr>
  </thead>
  <tbody>
   <?php
   $sql = "SELECT * FROM tb_deviceinfo,tb_clientinfo WHERE ci_id = di_clientId AND di_clientId =".$_POST[txtClientId]." ORDER BY di_deviceName,di_deviceId DESC";

   $rows = $db->query($sql);
   if($db->affected_rows > 0)
   {
   	while ($record = $db->fetch_array($rows)) 
	{
		if($record[di_deviceModel] > 0)
		{
			$sql1 = "SELECT * FROM tb_device_model WHERE id = ".$record[di_deviceModel];
			$rows1 = $db->query($sql1);
			if($db->affected_rows > 0)
			{
				$deviceModelRecord = $db->fetch_array($rows1);
				$devModel = $deviceModelRecord[tdm_aliasName];
			}			
		}else $devModel = "Not Selected";
		
		$getSubs = "SELECT * FROM tb_client_subscription WHERE tcs_isActive = 1 AND tcs_deviceId = ".$record[di_id];
		$resSubs = $db->query($getSubs);
		if($db->affected_rows >0)
		{
			$fetSubs = $db->fetch_array($resSubs);
			$renewDate = date("d-m-Y",strtotime("-1 days ".$fetSubs[tcs_renewalDateTo]));
		}
		else
		{
			$renewDate ="Nil";
		}
	?>
		<tr>
        <td><?php echo ucfirst($record[di_deviceId]);?></td>
        <td><?php echo ucfirst($record[ci_clientName]);?></td>
        <td><?php echo $record[di_imeiId];?> </td> 
        <td><?php echo ucfirst($record[di_deviceName]);?></td>
        <td><?php echo $record[di_mobileNo];?> </td>
        <td><?php echo ucfirst($devModel);?></td>
        <td><?php echo date("d-m-Y",strtotime($record[di_createDate]));?> </td>
        <td><?php echo date("d-m-Y",strtotime($renewDate));?></td>
		<td><img src="../../images/delete.gif" style="cursor:pointer;" width="45" height="16" border="0" onclick="funEditUser('<?php echo $record[di_id];?>','<?php echo $record[ci_id];?>','<?php echo $record[ci_modelType ];?>','?ch=viewClientDevice')" /></td>
   </tr>
    <?php
	}
	}
   ?>
   </tbody>
</table>
<form name="frmSubmit" id="frmSubmit" method="post">
	<input type="hidden" name="txtDeviceId" id="txtDeviceId" />
    <input type="hidden" name="txtClientId" id="txtClientId" />
	<input type="hidden" name="txtClientModel" id="txtClientModel" />
</form>